<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cn.com.dhcc.app.core.CoreEnv.CoreInitCtx"%>
<%@page import="cn.com.dhcc.ddi.datasource.vo.TableInfoVo" %>
<%@ include file="/WEB-INF/view/common/taglibs.jsp"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>${appname}</title>
<%@ include file="/WEB-INF/view/common/css_js_ace.jsp"%>
<%@ include file="/WEB-INF/view/common/css_js_ztree.jsp"%>
<script type="text/javascript" src="${ctx}/static/js/ddi/datasource/datasource.update.js"></script>
<script type="text/javascript">  
var isExit=true;
$(function(){
	$('.tipMe').tooltip({'placement':'top'});
	$(window).bind('beforeunload', function() {
		if (isExit) {
			return '您输入的内容尚未保存，确定离开此页面吗？';
		}
	});
	$(".chosen-select").chosen(); 
	$(".profile-info-name").css("width","115px");
});	
</script>
<style type="text/css">
.label.arrowed-nn-right:after {
    right: -9.4px;
    border-width: 10px 5px;
}
.chosen-container-single .chosen-search:after {
	display: none;
}
</style>
<body>
	<div class="main-container" id="main-container">
		<div class="main-container-inner">
			<div class="main-content" style="margin-left: 0px;">
				<div class="breadcrumbs" id="breadcrumbs">
					<ul class="breadcrumb">
						<li><i class="icon-home home-icon"></i> 首页</li>
						<li>数据源管理</li>
						<li class="active">修改</li>
					</ul>
				</div>
				<div class="page-content">
					<div class="row">
						<div class="col-xs-12">
							<div class="widget-box">
								<div class="widget-header widget-header-flat">
									<div class="widget-toolbar no-border">
										<a class="btn btn-primary btn-xs"
											href="${ctx}/data_source/data_source_list.do"> <i
											class="icon-arrow-left"></i> 返回&nbsp;&nbsp;
										</a>
									</div>
								</div>
								<div class="widget-body">
									<div class="widget-main">
								
										<form class="form-horizontal" role="form" id="inputForm"
											action="" method="post" onkeydown="if(event.keyCode==13)return false;">
											<span class="label label-primary arrowed-right arrowed-nn-right" style="margin-left:12px;">数据源基本信息</span>
											<div class="profile-user-info profile-user-info-striped" style="margin-top: 12px;">
												<div class="profile-info-row" style="height: 40px;">
													<div class="profile-info-name" style="padding-top:10px;"
														>数据源名称*</div>
													<div class="profile-info-value" style="vertical-align: middle;">
													    <input type="text" id="id" name="id" hidden="true" value="${vo.id }">
														<input type="text" class="col-xs-10 col-sm-5" name="name" id="name" value="${vo.name }" oldValue="${vo.name}"
															 title="必填项,数据源名称" placeholder="请输入数据源名称"
															datatype="require|ajax|safeString2" msg="请输入数据源名称|已经被占用|不允许输入特殊字符"
															require="true" maxlength="200" url="${ctx}/data_source/is_dsName_unique.do"/>
													</div>
												</div>
												<div class="profile-info-row" style="height: 40px;">
													<div class="profile-info-name" style="padding-top:10px;"
														for="form-field-1">所属节点*</div>
													<div class="profile-info-value" style="vertical-align: middle;">
														<select class="col-xs-10 col-sm-5" name="nodeId"
															id="nodeId" datatype="require" msg="请选择所属节点"
															require="true">
															<option value="">-请选择-</option>
															<c:forEach var="node" items="${nodeList}">
																<option value="${node.id}" <c:if test="${node.id eq vo.nodeId}">selected</c:if>>${node.name}</option>
															</c:forEach> 
														</select>
													</div>
												</div>
												<div class="profile-info-row" style="height: 40px;">
													<div class="profile-info-name" style="padding-top:10px;"
														for="form-field-1">数据源类型*</div>
													<div class="profile-info-value" style="vertical-align: middle;padding-top:10px;">
<%-- 														<select class="col-xs-10 col-sm-5" name="type" id="type" onchange="changeDataSourceType(this)">
															<option value="">-请选择数据源类型-</option>
															${app:ctoption("DS_TYPE",vo.type) }
														</select> --%>
														<input name="type" id="type" style="display: none;" value="${vo.type}">
														<span>${app:ctname("DS_TYPE",vo.type)}</span>
													</div>
												</div>
												<div class="profile-info-row" style="height: 56px;">
													<div class="profile-info-name" style="padding-top: 10px;" >备注</div>
													<div class="profile-info-value" style="vertical-align: middle;">
														<textarea class="col-sm-5" placeholder="请输入备注"
														name="remark" id="remark" title="备注">${vo.remark }</textarea>
													</div>
												</div>
											</div>
											<c:if test="${vo.type eq '1'}">
												<div id="database">
													<span class="label label-primary arrowed-right arrowed-nn-right" style="margin-top: 12px; margin-left:12px;">数据源地址</span>
													<div class="profile-user-info profile-user-info-striped" style="margin-top: 12px;">
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top:10px;"
																for="form-field-1">数据库类型*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<select class="col-xs-10 col-sm-5" name="dataSourceConfVo.dbType"
																	id="dbType" onchange="changeDbType(this)"
																	datatype="require" msg="请选择数据库类型" require="true">
																	<option value="">-请选择数据库类型-</option>
																	${app:ctoptionExt("DB_TYPE",vo.dataSourceConfVo.dbType,"","driverName,linkUrl") }
																</select>
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name"></div>
															<div class="profile-info-value" style="padding-top: 10px;">
																&nbsp;<font color="red">注：</font>数据库为SQLServer2000时,类型请选择sqlServer(2000)
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top:10px;"
																for="form-field-1">驱动类型*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<input type="text" class="col-xs-10 col-sm-5" readonly="readonly"
																	name="dataSourceConfVo.driverName" id="driverName" value="${vo.dataSourceConfVo.driverName}"  title="必填项,驱动类型"
																	placeholder="请输入驱动类型" datatype="require" msg="请输入驱动类型"
																	require="true" />
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top:10px;"
																for="form-field-1">URL*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<input type="text" class="col-xs-10 col-sm-5"
																	name="dataSourceConfVo.url" id="url" value="${vo.dataSourceConfVo.url}" title="必填项,连接URL"
																	placeholder="请输入连接URL" datatype="require" msg="请输入连接URL"
																	require="true" onkeyup="ifAgainExData()"/>
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top:10px;"
																for="form-field-1">用户名*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<input type="text" class="col-xs-10 col-sm-5"
																	name="dataSourceConfVo.userName" id="userName" value="${vo.dataSourceConfVo.userName}"
																	title="必填项,用户名" placeholder="请输入用户名" datatype="require"
																	msg="请输入用户名" require="true" maxlength="200" onkeyup="ifAgainExData()"/>
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;" id="mdDiv1">
															<div class="profile-info-name" style="padding-top:10px;"
																for="form-field-1">密码*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<input style="display: none"> <input
																	type="password" class="col-xs-10 col-sm-5" value="${vo.dataSourceConfVo.password}"
																	name="dataSourceConfVo.password" id="password"
																	title="必填项,密码" placeholder="请输入密码" msg="请输入密码" datatype="require"
																	maxlength="200" require="true" onkeyup="ifAgainExData()"/>
															</div>
														</div>
													</div>
													<div class="clearfix form-group mg-top12">
														<div class="text-center">
															<button type="submit" class="btn btn-sm btn-success"
																id="saveBtn" onclick="saveDB();">提交基本信息</button>&nbsp;&nbsp;
															<button type="button" class="btn btn-sm btn-success"
																id="testBtn" onclick="testDB(false);">测试连接</button>&nbsp;&nbsp;
															<button type="button" class="btn btn-sm btn-success"
																id="exDataBtn" onclick="extractMetadata();" disabled="disabled">
																重新抽取元数据</button>
														</div>
														<div class="row mg-top12">
															<div class="col-xs-3"></div>
															<div class="col-xs-9">
																<p><font color="#FF0000">提示：</font>重新抽取元数据会导致原有配置被覆盖并且可能用时较长，以下情况请只需要点击"提交基本信息"按钮即可：</p>
														        <p style="margin-left: 38px;">1.数据库地址或者用户不同，但数据库表同构。</p>
														        <p style="margin-left: 38px;">2.只修改了数据库登录密码。</p>
														        <p style="margin-left: 38px;">其他情况：如：数据库类型改变或元数据结构改变都需要重新抽取！</p>
															</div>	
														</div>
														
													</div>
												
													<div id="metadata">
													    <hr style="margin-left: 12px;margin-right: 12px;">
														<span class="label label-primary arrowed-right arrowed-nn-right" style="margin-left: 12px;">数据源数据结构</span>
														<div class="form-group" id="mdDiv1" style="vertical-align: middle;margin-top: 12px;">
															<div class="pull-left">
																<div class="pull-left" style="margin-left: 26px;margin-top: 4px;"
																	for="form-field-1">对象数据库逻辑结构：</div>
																<select class="pull-left chosen-select" style="width: 200px;" name="tableObject"
																		id="tableObject" onchange="changeTableInfo(this)">
																		<c:forEach items="${tableInfoList}" var="tableInfo" varStatus="status">
																			<c:choose>
																				<c:when test="${status.index eq 0}">
																				    <option selected = "selected" table_index="${status.index + 1}">${tableInfo.tableName}</option>
																				</c:when>
																				<c:otherwise>
																					<option table_index="${status.index + 1}">${tableInfo.tableName}</option>								
																				</c:otherwise>
																			</c:choose>
																		</c:forEach>
																</select>
															</div>
															<div class="pull-left" id="tableIndex" 
																	style="font-size: small; margin-top: 5px;margin-left: 20px;">1/${fn:length(tableInfoList)}</div>
	                                                        <div class="pull-left" style="margin-left: 26px;margin-top: 4px;">
	                                                          <span>schema：<span id="schemaId"></span></span>
	                                                        </div>
														</div>
														<div class="table-responsive" style="margin-left: 12px;margin-right: 12px;">
															<table
																class="table table-striped table-bordered table-hover">
																<thead>
																	<tr >
																		<th class="center">序号</th>
																		<th>项(英文名)</th>
																		<th>项(中文名)</th>
																		<th>类型</th>
																		<th>默认值</th>
																		<th>主键/唯一键</th>
																		<th>外键表</th>
																		<th>外键字段</th>
																		<th id="incrementalField_th" class="center">增量字段</th>
																	</tr>
																</thead>
																<c:forEach items="${tableInfoList}" var="tableInfo" varStatus="tablestatus">
																	<c:choose>
																		<c:when test="${tablestatus.index eq 0}">
																			<tbody id="${tableInfo.tableName}" name="${tableInfo.tableName}" tableId="${tableInfo.id}"
																				table_index="${tablestatus.index + 1}" schema="${tableInfo.schema}"
																				tableComment = "${tableInfo.tableComment}">
																				<script type="text/javascript">
																					lastSelectTable = "${tableInfo.tableName}";
																					$("#schemaId").html('${tableInfo.schema}');
																				</script>
																		</c:when>
																		<c:otherwise>
																			<tbody id="${tableInfo.tableName}" name="${tableInfo.tableName}" table_index="${tablestatus.index + 1}"
																				tableId="${tableInfo.id}" schema="${tableInfo.schema}" tableComment = "${tableInfo.tableComment}" 
																				style="display: none;">
																		</c:otherwise>
																	</c:choose>
				
																	<c:forEach items="${tableInfo.tableColVos}" var="colInfo" varStatus="status">
																		<tr>
																		    <td hidden="" id="col_id">${colInfo.id}</td>
																			<td class="center">${status.index + 1}</td>
																			<td id = 'colName' name = 'colName'>${colInfo.colName }</td>
																			<td id = 'colComment' name = 'colComment'><spqn class="tipMe" title="${colInfo.colComment}">${cm:lmitStrPx(colInfo.colComment,100)}</spqn></td>
																			<td id = 'colType' name = 'colType'>${colInfo.colType }</td>
																			<td id = 'colDefault' name = 'colDefault'><span class="tipMe" title="${colInfo.colDefault}">${cm:lmitStrPx(colInfo.colDefault,100)}</span></td>
																			<c:choose>
																				<c:when test="${colInfo.isPk eq 1}">
																					<td class='center' id = 'isPk' name ='isPk'><input type="checkbox" checked="checked" onclick="checkboxChange(this)" value="1"></td>
																				</c:when>
																				<c:otherwise>
																					<td class='center' id = 'isPk' name ='isPk'><input type="checkbox" onclick="checkboxChange(this)" value="0"></td>
																				</c:otherwise>
																			</c:choose>
																			<td>
																				<c:if test="${colInfo.isPk ne 1}">
																					<select name='fkTableName' id='fkTableName' style="width: 150px;" onchange='changefkTableUpdate(this)'>
																					   <option value="" >请选择</option>
																					   <c:forEach items="${tableInfoList}" var="tableInfo1">
																					        <c:if test="${tableInfo1.tableName ne tableInfo.tableName}">
																					        	<option <c:if test="${tableInfo1.tableName eq colInfo.fkTableName}">selected="selected"</c:if> value="${tableInfo1.tableName}">${tableInfo1.tableName }</option>
																					        </c:if>
																					   </c:forEach>
																				    </select>
																				</c:if>
																				<c:if test="${colInfo.isPk eq 1}">
																					<select name='fkTableName' id='fkTableName' style="width: 150px;" onchange='changefkTableUpdate(this)' hidden="true">
																					   <option value="" >请选择</option>
																					   <c:forEach items="${tableInfoList}" var="tableInfo1">
																					        <c:if test="${tableInfo1.tableName ne tableInfo.tableName}">
																					        	<option <c:if test="${tableInfo1.tableName eq colInfo.fkTableName}">selected="selected"</c:if> value="${tableInfo1.tableName}">${tableInfo1.tableName }</option>
																					        </c:if>
																					   </c:forEach>
																				    </select>
																				</c:if>
																			</td>
																			<td>
																				<c:if test="${colInfo.isPk ne 1}">
																					<select name='fkTablecolName' id='fkTablecolName' style='width: 150px;'>
																					   <option value="" >请选择</option>
																					     <c:if test="${colInfo.fkTablecolName ne null && colInfo.fkTablecolName ne ''}">
																					      <c:forEach items="${tableInfoList}" var="tableInfo1">
																					       <c:if test="${tableInfo1.tableName eq colInfo.fkTableName}">
																					           <c:forEach items="${tableInfo1.tableColVos}" var="col">
																				                     <option <c:if test="${col.colName eq colInfo.fkTablecolName}">selected="selected"</c:if> value="${col.colName}" table_name="${tableInfo1.tableName}">${col.colName}</option>
																				               </c:forEach>
																					        </c:if>
															        					    <c:if test="${tableInfo1.tableName ne colInfo.fkTableName}">
																				               <c:forEach items="${tableInfo1.tableColVos}" var="col">
																			                     <option value="${col.colName}" table_name="${tableInfo1.tableName}" style="display: none;">${col.colName}</option>
																				               </c:forEach>
																					        </c:if>
																				          </c:forEach>
																					     </c:if>
																					     
																					      <c:if test="${colInfo.fkTablecolName eq null || colInfo.fkTablecolName eq ''}">
																					      <c:forEach items="${tableInfoList}" var="tableInfo1">
																					         <c:forEach items="${tableInfo1.tableColVos}" var="col">
																				                 <option value="${col.colName}" table_name="${tableInfo1.tableName}" style="display: none;">${col.colName}</option>
																				             </c:forEach>
																				          </c:forEach>
																					     </c:if>
																					</select>
																				</c:if>
																				<c:if test="${colInfo.isPk eq 1}">
																					<select name='fkTablecolName' id='fkTablecolName' style='width: 150px;' hidden="true">
																					   <option value="" >请选择</option>
																					     <c:if test="${colInfo.fkTablecolName ne null && colInfo.fkTablecolName ne ''}">
																					      <c:forEach items="${tableInfoList}" var="tableInfo1">
																					       <c:if test="${tableInfo1.tableName eq colInfo.fkTableName}">
																					           <c:forEach items="${tableInfo1.tableColVos}" var="col">
																				                     <option <c:if test="${col.colName eq colInfo.fkTablecolName}">selected="selected"</c:if> value="${col.colName}" table_name="${tableInfo1.tableName}">${col.colName}</option>
																				               </c:forEach>
																					        </c:if>
																					        <c:if test="${tableInfo1.tableName ne colInfo.fkTableName}">
																				               <c:forEach items="${tableInfo1.tableColVos}" var="col">
																			                     <option value="${col.colName}" table_name="${tableInfo1.tableName}" style="display: none;">${col.colName}</option>
																				               </c:forEach>
																					        </c:if>
																				          </c:forEach>
																					     </c:if>
																					     
																					      <c:if test="${colInfo.fkTablecolName eq null || colInfo.fkTablecolName eq ''}">
																					      <c:forEach items="${tableInfoList}" var="tableInfo1">
																					         <c:forEach items="${tableInfo1.tableColVos}" var="col">
																				                 <option value="${col.colName}" table_name="${tableInfo1.tableName}" style="display: none;">${col.colName}</option>
																				             </c:forEach>
																				          </c:forEach>
																					     </c:if>
																					</select>
																				</c:if>
																			</td>
																			<td class="center" id = 'isIc' name = 'isIc'>
																			
																			    <c:if test="${fn:contains(fn:toUpperCase('number,int,tinyint,decimal,float,double,bigint,numeric'),fn:toUpperCase(colInfo.colType))||
																			                  fn:contains(fn:toUpperCase('timestamp,timestamp with local time zone,timestamp with time zone'),fn:toUpperCase(colInfo.colType))
																			    || fn:contains(fn:toUpperCase('date,datetime,time'),fn:toUpperCase(colInfo.colType))}">
																			   		<c:if test="${colInfo.isPk ne 1}">
																					    <c:choose>
																							<c:when test="${colInfo.isIc eq 1}">
																								<input type="radio" checked="checked" onclick="radioChange(this)" value="1">
																							</c:when>
																							<c:otherwise>
																								<input type="radio" onclick="radioChange(this)" value="0">
																							</c:otherwise>
																						</c:choose>
																				    </c:if>
																				    <c:if test="${colInfo.isPk eq 1}">
																					    <c:choose>
																							<c:when test="${colInfo.isIc eq 1}">
																								<input type="radio" checked="checked" onclick="radioChange(this)" value="1" hidden="true">
																							</c:when>
																							<c:otherwise>
																								<input type="radio" onclick="radioChange(this)" value="0" hidden="true">
																							</c:otherwise>
																						</c:choose>
																				    </c:if>
																			    </c:if>
																			    
																			</td>
																		</tr>
																	</c:forEach>
																	</tbody>
																</c:forEach>
															</table>
														</div>
													</div>
												</div>
											</c:if>	
											<c:if test="${vo.type eq '2'}">
												<div id="localdisk">'
													<span class="label label-primary arrowed-right arrowed-nn-right" style="margin-top: 12px; margin-left:12px;">数据源地址</span>
													<div class="profile-user-info profile-user-info-striped" style="margin-top: 12px;">
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top: 10px;">磁盘目录*</div>
															<div class="profile-info-value" style="vertical-align: middle;">
																<input type="text" class="col-xs-10 col-sm-5"
																	name="dataSourceConfVo.diskDirectory" id="diskDirectory" title="必填项,磁盘目录"
																	placeholder="请输入磁盘目录"
																	datatype="require|ajax" 
																	 msg="请输入磁盘目录|目录不存在"
																	require="true" maxlength="200"
																	value="${vo.dataSourceConfVo.diskDirectory}"
																	url="${ctx}/data_source/is_local_disk_exists.do" />
															</div>
														</div>
														<div class="profile-info-row" style="height: 40px;">
															<div class="profile-info-name" style="padding-top: 10px;">是否解析数据内容*</div>
															<div class="profile-info-value" style="vertical-align: middle;">&nbsp;&nbsp;
																<input type="radio" name="dataSourceConfVo.ifParseData" value="1" <c:if test="${vo.dataSourceConfVo.ifParseData eq '1'}">checked="checked"</c:if> onclick="parseDiskData(this)">是&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
																<input type="radio" name="dataSourceConfVo.ifParseData" value="0" <c:if test="${vo.dataSourceConfVo.ifParseData eq '0'}">checked="checked"</c:if> onclick="parseDiskData(this)">否
															</div>
														</div>
													</div>
												</div>
												<div id="ifParseDiskDataHtml">
												</div>
											</c:if>
											<div class="clearfix form-group mg-top12" id="sbmDiv">
												<div class="text-center">
													<button type="submit" class="btn btn-sm btn-info" id="sbmBtn"
														onclick="submitBtn()"><i class="icon-ok bigger-110"></i>提交</button>
												</div>
											</div>	
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>